import {
  getCollection,
  delCollection
} from '../../api/api.js'
// pages/wdsc/wdsc.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    skip: 0,
    page: 0,
    total: 0,
    jlf:false
  },

  //点击去课程
  goKc(e) {
    console.log(e);
    wx.setStorageSync('kc', e.currentTarget.dataset.item.course_id)
    wx.navigateTo({
      url: '../../pages/kecheng/kecheng',
    })
  },

  //删除收藏
  del(e) {
    const index = e.currentTarget.dataset.index
    wx.showModal({
      title: '删除收藏',
      confirmColor: '#009900',
      success: (res => {
        if(res.confirm){
      delCollection(this.data.list[index]._id).then(res => {
          console.log(res);
          this.data.list.splice(index, 1)
          this.setData({
            list: this.data.list
          })
          wx.showToast({
            title: '删除成功',
            icon: 'none'
          })
        })
        }      
      })
    })
  },

  //获取收藏
  getCollection() {
    this.setData({
      jlf:true
    })
    const openid = wx.getStorageSync('openid')
    console.log(this.data.skip);
    getCollection(openid, this.data.skip).then(res => {
      console.log(res);

      this.setData({
        list: this.data.list.concat(res.data.data),
        total: res.data.total,
        jlf:false
      })
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    wx.showLoading({
      title: '加载中',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
    this.getCollection()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      list: [],
      skip: 0,
      page: 0,
      total: 0,
      jlf:false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.jlf){
      return
    }
    if (this.data.list.length >= this.data.total) {
      wx.showToast({
        title: '数组加载完毕',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '努力加载中',
    })
    this.setData({
      page: this.data.page + 1
    })
    this.data.skip = this.data.page * 10
    this.getCollection()
  },

  //分享微信好友
  onShareAppMessage:function(from){
    if(from.from === 'menu'){
      return {
        title:'科云视频库，大量IT学习资源等你来',
        path:'/pages/index/index'
      }
    }
  },
  //分享朋友圈
  onShareTimeline:function(){
    return {
      title:'科云视频库，大量IT学习资源等你来',
      imageUrl:"https://7975-yueyun188-0gdwo4vh4afabc50-1306555544.tcb.qcloud.la/cloudbase-cms/upload/2022-03-09/mras7605qjl0klsizgkbvfga5nrpsian_.png",
      path:'/pages/index/index'
    }
  }
})